package com.by.controller;

import com.alibaba.fastjson.JSON;
import com.by.domain.Product;
import com.by.impl.ProductInterfaceImpl;
import com.by.mapper.ProductInterface;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

/**
 * <p><p/>
 *
 * @author boyin
 * @ClassName ProductAddController
 * @Description 添加商品功能
 * @date 2024/12/25 22:16
 */
@WebServlet("/product/addProducts")
public class ProductAddController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //添加商品
        String name =new String(req.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
        String stock = req.getParameter("stock");
        String price = req.getParameter("price");
        ProductInterface productInterface = new ProductInterfaceImpl();
        Product product = new Product();
        product.setId(UUID.randomUUID().toString());
        product.setName(name);
        product.setStock(Integer.valueOf(stock));
        product.setPrice(Integer.valueOf(price));
        productInterface.addProduct(product);
        //重定向回到主页
        resp.sendRedirect("http://localhost:8080/demo/product/index.html");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
